﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agr.CQRS.Domain.Commands;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Agr.CQRS.Infrastructure.Commands;
using Ninject;
using Ninject.Modules;
using Agr.CQRS.Infrastructure.ServiceBus;
using System.Threading;
using Agr.CQRS.Domain.Repository;

namespace Tests.Agr.CQRS.InfraStructure.Commands
{
    [TestClass]
    public class When_CommandRouter_Gets_New_Command
    {
        [TestInitialize]
        public void TestInitialize()
        {
            //Given
            var kernel = new StandardKernel(new DiModule());
            var commandRouter = kernel.Get<CommandRouter>();
            commandRouter.Start();
            var sb = kernel.Get<IServiceBus>();
            sb.Publish<Command>(new TestCommand());
        }

        [TestMethod]
        public void Then_The_Corresponding_CommandHandler_Is_Called()
        {
            Thread.Sleep(100); // because servicebus fires event async
            Assert.IsTrue(TestCommandHandler.ExecuteCalled);
        }


        public class TestCommand : Command { }

        public class TestCommandHandler : CommandHandler<TestCommand>
        {
            public static bool ExecuteCalled { get; set; }
            protected override void ExecuteInternal(TestCommand command)
            {
                ExecuteCalled = true;
            }
        }

        public class DiModule : NinjectModule {

            public override void Load()
            {
                Bind<IServiceBus>().To<ServiceBus>().InSingletonScope();
                Bind<IDomainRepository>().To<MockDomainRepository>().InSingletonScope();
            }
        }
    }

}
